﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiveUp.Net.Mail;
using System.ComponentModel;

namespace MailProcessing
{
    public class Pop3Controller
    {


        /// <summary>
        /// Attribute for Pop3Client.
        /// </summary>
        private Pop3Client _pop3Client;

        /// <summary>
        /// Default constructor.
        /// </summary>
        /// <param name="accountInfo">The default account information.</param>
        public Pop3Controller(MailAccountInfo accountInfo)
        {
            this.Connect(accountInfo);
        }
        public void UpdateStats()
        {
            this._pop3Client.UpdateStats();  
        }
        /// <summary>
        /// Connect the pop3 client.
        /// </summary>
        /// <param name="accountInfo">The account information.</param>
        public void Connect(MailAccountInfo accountInfo)
        {
            if (accountInfo != null)
            {
                this._pop3Client = new Pop3Client();

                int port = accountInfo.InPort;
                bool ssl = accountInfo.IncomingIsSSL;
                string serverName = accountInfo.IncomingServerName;
                string user = accountInfo.EmailAddress;
                string password = accountInfo.Password;  
                if (ssl)
                {
                    this._pop3Client.ConnectSsl(serverName, port, user, password);
                }
                else
                {                    
                    this._pop3Client.Connect(serverName, port, user, password);                    
                }
            }
        }

        /// <summary>
        /// Method for disconnect from Pop3 protocol.
        /// </summary>
        public void Disconnect()
        {
            if (this._pop3Client != null && this._pop3Client.IsConnected)
            {
                this._pop3Client.Disconnect();
            }
        }

        public BindingList<Header> GetAllMessageHeader()
        {
            BindingList<Header> ret = new BindingList<Header>();
            for (int i = 1; i <= this._pop3Client.MessageCount; i++)
            {              
                    Header header = this._pop3Client.RetrieveHeaderObject(1);
                    ret.Add(header);                
            }
            return ret;
        }
        public Message GetMessage(int index)
        {
            return this._pop3Client.RetrieveMessageObject(index);
        }
        public IList<Message> GetAllMessages(int index)
        {
            IList<Message> ret = new List<Message>();

            int messageCount = this._pop3Client.MessageCount;
            for (int i = index; i <= this._pop3Client.MessageCount; i++)
            {
                Message message = this._pop3Client.RetrieveMessageObject(i);
                ret.Add(message);
            }
            return ret;
        }
        public IList<Message> GetAllMessages()
        {
            IList<Message> ret = new List<Message>();

            int messageCount = this._pop3Client.MessageCount;
            for (int i = 1; i <= this._pop3Client.MessageCount; i++)
            {
                Message header = this._pop3Client.RetrieveMessageObject(i);
                ret.Add(header);
            }
            return ret;
        }
        
        /// <summary>
        /// Method for retrieve the mail messages for POP3 protocol.
        /// </summary>
        /// <param name="beginIndex">The begin index for retrieve the message.</param>
        /// <param name="increment">The increment for begin index for retrieve messages.</param>
        /// <returns>The mail messages.</returns>
        //public BindingList<Message> RetrieveMessages(int beginIndex, int increment)
        //{
        //    BindingList<Message> ret = new BindingList<Message>();

        //    Message mailMessage;
        //    Header header;

        //    int messageCount = this._pop3Client.MessageCount;
        //    for (int i = beginIndex; i <= beginIndex + increment; i++)
        //    {
        //        if (i <= messageCount)
        //        {
        //            header = this._pop3Client.RetrieveHeaderObject(i);

        //            ret.Add(header);
        //        }
        //    }
        //    return ret;
        //}

        /// <summary>
        /// Delete a message basend on message identifier.
        /// </summary>
        /// <param name="messageId">The message identifier.</param>
        public void DeleteMessage(int index)
        {
           
            int messageCount = this._pop3Client.MessageCount;
            //for (int i = 1; i <= messageCount; i++)
            //{
            //    Header header = this._pop3Client.RetrieveHeaderObject(i);
            //    if (header.MessageId.Equals(messageId))
            //    {
            //        index = i;
            //        break;
            //    }
            //}

            if (index > 0 && index <= messageCount)
            {
                this._pop3Client.DeleteMessage(index);
            }
        }

        /// <summary>
        /// Get a mail message basend on message identifier.
        /// </summary>
        /// <param name="mailMessage">The mail message.</param>
        /// <returns>The ActiveUp.Net.Mail.Message object.</returns>
        //public ActiveUp.Net.Mail.Message GetMessage(MailMessage mailMessage)
        //{
        //    ActiveUp.Net.Mail.Message ret = null;
        //    ActiveUp.Net.Mail.Message message = null;
        //    int messageCount = this._pop3Client.MessageCount;

        //    int upIndex = mailMessage.BaseIndex;
        //    int downIndex = mailMessage.BaseIndex;

        //    // search up and down.
        //    while (downIndex > 0 || upIndex <= messageCount)
        //    {

        //        if (downIndex > 0 && downIndex <= messageCount)
        //        {
        //            message = this._pop3Client.RetrieveMessageObject(downIndex);
        //            if (message.MessageId != null && message.MessageId.Equals(mailMessage.Id))
        //            {
        //                ret = message;
        //                break;
        //            }
        //        }

        //        if (upIndex <= messageCount && upIndex > 0)
        //        {
        //            message = this._pop3Client.RetrieveMessageObject(upIndex);
        //            if (message.MessageId != null && message.MessageId.Equals(mailMessage.Id))
        //            {
        //                ret = message;
        //                break;
        //            }
        //        }

        //        downIndex--;
        //        upIndex++;
        //    }

        //    return ret;
        //}

        /// <summary>
        /// Method for get the message count.
        /// </summary>
        /// <returns>The message count number.</returns>
        public int GetMessageCount()
        {
            return this._pop3Client.MessageCount;
        }

        //public string[] GetAllMailBoxes()
        //{
        //    //this._pop3Client.
        //}

    }
}
